package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);

        orderService.submit(orders);

        return R.success("订单提交成功!!");
    }

    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page,
                                Integer pageSize, Long number,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("请求参数{},{},{},{},{}",page,pageSize,number,beginTime,endTime);
        Page<Orders> list = orderService.paginationQuery(page,pageSize,number,beginTime,endTime);
        return R.success(list);
    }

    /**
     * 修改订单状态信息
     */
    @PutMapping
    public R<String> updInfo(@RequestBody Orders orders) {
        log.info("订单状态修改参数{},{}",orders.getStatus(),orders.getId());
        Boolean isSuccess = orderService.updStateInfo(orders);
        if (isSuccess) {
            return R.success("修改订单状态成功!!");
        } else {
            return R.error("修改状态时发生了错误..");
        }
    }

    /**
     * 移动端最新订单查询
     */
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(Integer page,Integer pageSize) {
        return R.success(orderService.userPage(page,pageSize));
    }

    /**
     * 再来一单
     */
    @PostMapping("/again")
    public R<String> againOrder(@RequestBody Orders orders) {
        log.info("check whether the id can be injected successfully:{}",orders.getId());
        orderService.comeAgain(orders);
        return R.success("商品添加购物车成功!!!");
    }
}
